package com.chagee.store.operation.server.service.base;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chagee.store.operation.common.constant.SupplierWorkOrderConstant;
import com.chagee.store.operation.common.exception.BizErrorCode;
import com.chagee.store.operation.common.exception.BizException;
import com.chagee.store.operation.server.event.dao.WorkOrderReceiverMapper;
import com.chagee.store.operation.server.entity.WorkOrderReceiverDO;
import com.chagee.store.operation.server.event.service.WorkOrderReceiverBaseService;
import com.chagee.store.operation.server.model.param.GetCompanyStoreParam;
import com.chagee.store.operation.server.model.param.SupplierWorkOrderQueryParam;
import com.chagee.store.operation.server.model.vo.CompanyStorePageVO;
import com.chagee.store.operation.server.model.vo.CompanyVO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName SupplierWorkOrderBaseService  //类名称
 * @Description: //类描述:供应商获取工单信息接口实现
 * @Author: Jacky-zhan	//作者
 * @CreateDate: 2024/04/25 14:03	//创建时间
 * @UpdateUser: Jacky-zhan	//作者
 * @UpdateDate: 2024/04/25 14:03	//更新时间
 * @UpdateRemark: 更新的信息
 * @Version: 1.0    //版本号
 */
@Service
@RequiredArgsConstructor
public class SupplierWorkOrderBaseService extends ServiceImpl<WorkOrderReceiverMapper, WorkOrderReceiverDO> {

	@Resource
	private WorkOrderReceiverBaseService workOrderReceiverBaseService;

	@Resource
	private WorkOrderReceiverMapper workOrderReceiverMapper;

	/**
	 * 查询接单人信息列表
	 *
	 * @param queryParameter 搜索条件
	 * @param page           分页条件
	 * @return 分页数据
	 */
	public Page<WorkOrderReceiverDO> pageQueryList(SupplierWorkOrderQueryParam queryParameter, Page<WorkOrderReceiverDO> page) {
		return workOrderReceiverBaseService.queryWorkOrderByBaseParam(queryParameter, page);
	}

	/**
	 * 通过 门店编号和状态不是已完成/已取消 查询接单人信息
	 *
	 * @param storeCode 门店编号
	 * @return 接单人信息
	 */
	public List<WorkOrderReceiverDO> getAllByStoreCode(String storeCode) {
		return workOrderReceiverBaseService.getAllByStoreCode(storeCode);
	}

	/**
	 * 插入数据
	 *
	 * @param workOrderReceiverDO DO对象
	 */
	public void insert(WorkOrderReceiverDO workOrderReceiverDO) {
		workOrderReceiverBaseService.insert(workOrderReceiverDO);
	}

	/**
	 * 根据接单人id+门店编号+工单编号+状态:not in(4,5) 混合查询接单人信息
	 *
	 * @param receiverId      接单人id
	 * @param storeCode       门店编号
	 * @param workOrderNumber 工单编号
	 * @return list
	 */
	public List<WorkOrderReceiverDO> getByFix(Long receiverId, String storeCode, String workOrderNumber) {
		return workOrderReceiverBaseService.getByFix(receiverId, storeCode, workOrderNumber);
	}

	/**
	 * 获取获取 子公司/门店 （主账户查询供应商的子公司/门店，子账户查询自己对应的子公司/门店）
	 *
	 * @param getCompanyStoreParam 获取 子公司/门店 入参
	 * @return list
	 */
	public List<CompanyVO> getCompanyList(GetCompanyStoreParam getCompanyStoreParam) {
		List<CompanyVO> companyVOList = new ArrayList<>();
		if (Objects.isNull(getCompanyStoreParam)) {
			//入参为空
			throw new BizException(BizErrorCode.PARAMETER_IS_NULL);
		}
		//userType=1是主账户，查询供应商的所有数据;
		Integer userType = getCompanyStoreParam.getUserType();
		if (Objects.nonNull(userType) && SupplierWorkOrderConstant.USER_TYPE_MAIN_ACCOUNT.equals(String.valueOf(userType))) {
			//供应商id
			Long supplierId = getCompanyStoreParam.getSupplierId();
			if (Objects.nonNull(supplierId)) {
				List<CompanyVO> workOrderReceiverDOList = workOrderReceiverBaseService.getBySupplierId(supplierId);
				if (CollectionUtils.isNotEmpty(workOrderReceiverDOList)) {
					workOrderReceiverDOList.forEach(workOrderReceiverDO -> {
						CompanyVO companyVO = new CompanyVO();
						if (StringUtils.isNotBlank(workOrderReceiverDO.getCompanyId()) && StringUtils.isNotBlank(workOrderReceiverDO.getCompanyName())) {
							companyVO.setSupplierId(supplierId);
							companyVO.setCompanyId(workOrderReceiverDO.getCompanyId());
							companyVO.setCompanyName(workOrderReceiverDO.getCompanyName());
							companyVOList.add(companyVO);
						}
					});
				}
			}
		}
		//userType=2是子账户，查询该接单人的数据门店对应的子公司
		if (Objects.nonNull(userType) && SupplierWorkOrderConstant.USER_TYPE_SUB_ACCOUNTS.equals(String.valueOf(userType))) {
			//接单人id
			Long receiverID = getCompanyStoreParam.getReceiverID();
			List<CompanyVO> workOrderReceiverDOList = workOrderReceiverBaseService.getByReceiverID(receiverID);
			if (CollectionUtils.isNotEmpty(workOrderReceiverDOList)) {
				workOrderReceiverDOList.forEach(workOrderReceiverDO -> {
					CompanyVO companyVO = new CompanyVO();
					if (StringUtils.isNotBlank(workOrderReceiverDO.getCompanyId()) && StringUtils.isNotBlank(workOrderReceiverDO.getCompanyName())) {
						companyVO.setReceiverId(receiverID);
						companyVO.setCompanyId(workOrderReceiverDO.getCompanyId());
						companyVO.setCompanyName(workOrderReceiverDO.getCompanyName());
						companyVOList.add(companyVO);
					}
				});
			}
		}
		return companyVOList;
	}

	/**
	 * 获取获取 子公司-门店 （主账户查询供应商的子公司-门店，子账户查询自己对应的子公司-门店）
	 *
	 * @param getCompanyStoreParam 获取 子公司-门店 入参
	 * @param page                 分页参数
	 * @return page对象
	 */
	public Page<CompanyStorePageVO> getCompanyStoreList(GetCompanyStoreParam getCompanyStoreParam, Page<WorkOrderReceiverDO> page) {
		return workOrderReceiverBaseService.getCompanyStoreList(getCompanyStoreParam, page);
	}

	/**
	 * 根据工单编号+待执行状态查询
	 *
	 * @param workOrderNumber 工单编号
	 * @return 工单信息
	 */
	public List<WorkOrderReceiverDO> selectListByWorkOrderNumber(String workOrderNumber) {
		return workOrderReceiverBaseService.selectListByWorkOrderNumber(workOrderNumber);
	}

	/**
	 * 根据工单id+待执行状态查询
	 *
	 * @param workOrderId 工单id
	 * @return 工单信息
	 */
	public List<WorkOrderReceiverDO> selectListByWorkOrderId(Long workOrderId) {
		return workOrderReceiverBaseService.selectListByWorkOrderId(workOrderId);
	}

	/**
	 * 通过工单编号查询接单人信息
	 *
	 * @param workOrderNumber 工单编号
	 * @return 接单人信息
	 */
	public List<WorkOrderReceiverDO> getAllByWorkOrderNumber(String workOrderNumber) {
		return workOrderReceiverBaseService.getAllByWorkOrderNumber(workOrderNumber);
	}

}
